package cn.maihe.elg.operation.centers.epoint.channel.service;

import cn.maihe.elg.operation.centers.epoint.channel.EpointBaseChannelService;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.dto.channel.PTBaohanNoticeReqDTO;
import cn.maihe.elg.operation.centers.epoint.enums.EpointChannelMethod;
import cn.maihe.elg.operation.centers.epoint.enums.EpointRespCodeEnum;
import cn.maihe.elg.operation.centers.supports.model.bo.PushCenterGuaranteeBo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeVo;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description 出函机构开户完推送 电子保函至平台侧
 * @Author WJH
 * @Date 2021/04/21
 */
@Service
@Slf4j
public class PTBaohanNoticeChannelService extends EpointBaseChannelService<PTBaohanNoticeReqDTO, EpointBaseRespDTO> {

    @Resource
    private ElgResourceService elgResourceService;
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;

    @Override
    protected EpointChannelMethod methodEnum() {
        return EpointChannelMethod.baohannotice;
    }


    public InnerBaseResp<String> pushGuarantee(PushCenterGuaranteeBo bo) {
        GuaranteeVo vo = bo.getGuaranteeVo();
        String applyStatus = bo.getApplyStatus();
        CenterNoEnum centerNoEnum = this.getCenterNoEnumById(vo.getCenterId());
        PTBaohanNoticeReqDTO reqDTO = new PTBaohanNoticeReqDTO();
        reqDTO.setApplyno(vo.getCenterOrderId());
        reqDTO.setInstname(vo.getGuarantorName());
        reqDTO.setBaohanno(vo.getGuaranteeNo());
        if (vo.getGuaranteeTime() == null) {
            vo.setGuaranteeTime(new Date());
        }
        reqDTO.setGeneratetime(DateUtil.formateDate(vo.getGuaranteeTime(), "yyyy-MM-dd HH:mm:ss"));
//        reqDTO.setGeneratetime(vo.getGuaranteeTime() != null ? DateUtil.formateDate(vo.getGuaranteeTime(), "yyyy-MM-dd HH:mm:ss") : null);
        reqDTO.setApplystatus(applyStatus);
        //0.01% 转0.0001
        //reqDTO.setRate(getPercentageToDecimal(vo.getRate()));
        InnerBaseResp<String> innerBaseResp = null;

        AcceptOrderInfoDO acceptOrderInfo = acceptOrderInfoService.getByAcceptOrderNo(vo.getAcceptOrderNo());
        Assert.notNull(acceptOrderInfo, "查无此受理订单记录");
        try {

            EpointBaseRespDTO resp = this.doRequest(centerNoEnum, reqDTO);
            if (EpointRespCodeEnum.isSuccess(resp.getCode())) {
                innerBaseResp = InnerBaseResp.success(reqDTO.getBaohanno());
            } else {
                innerBaseResp = InnerBaseResp.error(resp.getMessage());
            }
        } catch (Exception e) {
            log.error("开函结果通知 >> [{}] 异常:{}", centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }

        return innerBaseResp;
    }

}
